#ifndef SHAFILE_H
#define SHAFILE_H

#include "../../include/hge.h"

class shaFileReader
{
public:
#ifdef UNICODE
	shaFileReader(const wchar_t* file = NULL) { data = src = NULL; curPos = size = 0; hge = hgeCreate(HGE_VERSION); open(file); }
	~shaFileReader() { if (src) { hge->Resource_Free(src); } hge->Release(); } 

	bool	open(const wchar_t* file)
	{
		close();
		if (file)
		{
			data = src = (char*)hge->Resource_Load(file, &size);
			curPos = 0;
		}
		return true && data;
	}
#else
	shaFileReader(const char* file = NULL) { data = src = NULL; curPos = size = 0; hge = hgeCreate(HGE_VERSION); open(file); }

	bool	open(const char* file)
	{
		close();
		if (file)
		{
			data = src = (char*)hge->Resource_Load(file, &size);
			curPos = 0;
		}
		return true && data;
	}
#endif
	void	close() { if (src) { hge->Resource_Free(src); } src = data = NULL;}
	DWORD	read(void* ptr, DWORD readSize)
	{
		curPos += readSize;
		if (curPos > size)
		{
			readSize = size - curPos + readSize;
			if (size + readSize < curPos)
			{
				readSize = 0;
			}
		}
		if (data)
		{
			memcpy(ptr, data, readSize);
			data += readSize;
			return readSize;
		}
		return 0;
	}
	DWORD	getPos() {return curPos;}
	DWORD	getSize() {return size;}
	bool	isEOF() { return curPos > size; }
    void	seek(DWORD pos) { data += pos; curPos += pos; }
protected:
	char*	data;
	char*	src;
	DWORD	size;
	DWORD	curPos;

	HGE*	hge;
};

#endif